#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2025/11/3
# @USER    : Shengji He
# @File    : temp.py
# @Software: PyCharm
# @Version  : Python-
# @TASK:
from PIL import Image, ImageDraw

import config
import core_functions as core


def process_world_area():
    """世界区域"""
    # current_source_name = "世界区域"
    # sources_cfg = getattr(config, 'SOURCES', None)
    # SOURCES = {}
    # for menu_name, spec in sources_cfg.items():
    #     SOURCES[menu_name] = core.create_satellite_url_generator(spec, config.SATELLITE_IMAGE_DELAY_MINUTES)
    #
    # current_source_object = SOURCES[current_source_name]
    #
    # width, height = core.get_screen_resolution()
    #
    # final_url = current_source_object()

    file = r'.\Pictures\AutoWallpaper\original_wallpaper.jpg'

    with Image.open(file) as img:
        img_width, img_height = img.size

        # (left, upper, right, lower)
        # img_cropped = img.crop((0, 80, img_width, img_height - 96))
        img_cropped = img.crop((0, 20, img_width, img_height - 950))

        draw = ImageDraw.Draw(img_cropped)
        draw.rectangle((0, 0, 1750, 525), (0, 0, 0))
        img_cropped.show()
        pass

    pass


def icon_nuitka():
    import ctypes

    class IconDirectoryHeader(ctypes.Structure):
        _fields_ = [
            ("reserved", ctypes.c_short),
            ("type", ctypes.c_short),
            ("count", ctypes.c_short),
        ]

    class IconDirectoryEntry(ctypes.Structure):
        _fields_ = [
            ("width", ctypes.c_char),
            ("height", ctypes.c_char),
            ("colors", ctypes.c_char),
            ("reserved", ctypes.c_char),
            ("planes", ctypes.c_short),
            ("bit_count", ctypes.c_short),
            ("image_size", ctypes.c_int),
            ("image_offset", ctypes.c_int),
        ]

    def readFromFile(readable, c_struct):
        """Read ctypes structures from input."""

        result = c_struct()
        chunk = readable.read(ctypes.sizeof(result))
        ctypes.memmove(ctypes.byref(result), chunk, ctypes.sizeof(result))
        return result

    icon_path = './icon_fixed.ico'
    images = []
    with open(icon_path, "rb") as icon_file:
        header = readFromFile(icon_file, IconDirectoryHeader)
        icons = [
            readFromFile(icon_file, IconDirectoryEntry)
            for _i in range(header.count)
        ]
        for icon in icons:
            icon_file.seek(icon.image_offset, 0)
            images.append(icon_file.read(icon.image_size))
        pass
    try:
        img = Image.open('./icon.ico')
        print(f"尺寸: {img.size}")
        print(f"模式: {img.mode}")
        # 可以重新保存为正确的ICO格式
        img.save('./icon_fixed.ico', format='ICO')
        print("已重新保存为正确的ICO格式")
    except Exception as e:
        print(f"PIL打开失败: {e}")
    pass


if __name__ == '__main__':
    icon_nuitka()
    # process_world_area()

    print('done')
